package com.ruoyi.develop.goodsSupplier.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.goodsSupplier.domain.GoodsSupplier;
import com.ruoyi.develop.goodsSupplier.service.IGoodsSupplierService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 供应商信息管理Controller
 * @author qh
 * @date 2023-01-05
 */
@RestController
@RequestMapping("/goodsSupplier/goodsSupplier")
public class GoodsSupplierController extends BaseController {
    @Autowired
    private IGoodsSupplierService goodsSupplierService;

    /**
     * 查询供应商信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSupplier:query')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSupplier entity) {
        startPage();
        List<GoodsSupplier> list = goodsSupplierService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出供应商信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSupplier:export')")
    @Log(title = "供应商信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsSupplier entity) {
        List<GoodsSupplier> list = goodsSupplierService.selectList(entity);
        ExcelUtil<GoodsSupplier> util = new ExcelUtil<GoodsSupplier>(GoodsSupplier. class);
        util.exportExcel(response, list, "供应商信息管理数据");
    }

    /**
     * 获取供应商信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSupplier:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsSupplierService.selectById(id));
    }

    /**
     * 新增供应商信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSupplier:add')")
    @Log(title = "供应商信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsSupplier entity) {
        if (UserConstants.NOT_UNIQUE.equals(goodsSupplierService.checkNameUnique(entity.getName()))) {
            return AjaxResult.error("新增供应商'" + entity.getName() + "'失败，供应商已存在");
        }
        return toAjax(goodsSupplierService.insert(entity));
    }

    /**
     * 修改供应商信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSupplier:edit')")
    @Log(title = "供应商信息管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsSupplier entity) {
        return toAjax(goodsSupplierService.update(entity));
    }

    /**
     * 删除供应商信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSupplier:remove')")
    @Log(title = "供应商信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(goodsSupplierService.deleteByIds(ids));
    }


    @GetMapping(value = "/suppNames")
    public AjaxResult suppNames(GoodsSupplier param) {
        startPage();
        List<SysDictData> nameList = goodsSupplierService.selectAllSuppliers(param);
        Map<String, Object> retMap = new HashedMap();
        retMap.put("nameList", nameList);
        return AjaxResult.success(retMap);
    }

    @Log(title = "供应商信息导入数据", businessType = BusinessType.INSERT)
    @PostMapping("/importData")
    public AjaxResult importData(GoodsSupplier entity,MultipartFile file) {
        Map<String, Object> error = new HashMap<>();
        error = goodsSupplierService.importData(entity,file);
        error.put("code", 200);
        return AjaxResult.success(error);
    }
}
